﻿//进程自动关闭器,用于瞬间关闭桌面上打开的无数多个IE窗口，一键搞定
using System;
using System.Diagnostics;
using System.IO;   //文件操作
using System.Linq;
using System.Windows.Forms;
using System.Xml;
namespace killprocess
{
    public partial class Kill_Process : Form
    {
        public Kill_Process()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                if (!File.Exists( System .Environment .GetEnvironmentVariable("WINDIR") +"\\nathan_pro_kill.xml"))   //如果processes.xml文件不存在则进行创建
                {
                    create_xml();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            XmlDocument process = new XmlDocument();
            process.Load(System .Environment .GetEnvironmentVariable("WINDIR") +"\\nathan_pro_kill.xml");

            //set lb_pro_tokill items contents
            XmlNodeList processes = process.GetElementsByTagName ("pro_name");
            lb_pro_tokill.Items.Clear();
            foreach (XmlElement xl in processes)
                lb_pro_tokill.Items.Add(xl.InnerText);

            //set autokill time
            XmlElement time = (XmlElement)process.SelectSingleNode("kill_processes/time");
            tb_time.Text = time.InnerText;

            process_search();               //搜索现有进程
            this.ShowInTaskbar = false;      //任务栏不显示
        }

        private void process_search()    
        {
        
             Process[] localAll = Process.GetProcesses();

             string[] p_name = new string[localAll.Count()];   //将进程名保存到数组

             for (int i = 0; i < localAll.Count(); i++)
             {
                 string str_p = localAll[i].ToString();
                 int p_s = str_p.IndexOf("(");
                 int p_e = str_p.IndexOf(")");
                 p_name [i]=   (str_p .Substring (p_s+1 ,p_e-p_s-1 ));
             }
             Array.Sort(p_name);     //对进程名进行排序
             for (int i = 0; i < localAll.Count(); i++)
             {
                 listBox1.Items.Add(p_name[i]);
             }
        }

        private void process_kill()    //关闭指定进程
        {

            for (int i = 0; i < lb_pro_tokill.Items.Count; i++)
            {
                Process[] pro_ie = Process.GetProcessesByName(  lb_pro_tokill.Items[i].ToString().Trim ());
                foreach (Process pie in pro_ie)
                    try { pie.Kill(); }
                    catch { }
            }
        }



        private void btn_pro_kill_do_Click(object sender, EventArgs e)  //立即执行进程关闭
        {
            process_kill();
            btn_pro_refresh_Click(null,null);
        }

        private void timer1_Tick(object sender, EventArgs e)   //启用定时，每一秒进行一次进程关闭
        {
            process_kill();
        }

        private void btn_pro_refresh_Click(object sender, EventArgs e) //刷新进程
        {
            listBox1.Items.Clear();
            process_search();
        }

        private void btn1_auto_on_Click(object sender, EventArgs e)  //自动开始(启动定时器)
        {
            timer1.Interval = Convert.ToInt16(tb_time.Text) * 1000;
            timer1.Enabled = true;
            btn1_auto_on.Enabled = false;
            btn1_auto_off.Enabled = true;
        }

        private void btn1_auto_off_Click(object sender, EventArgs e) //自动结束(关闭定时器)
        {
            timer1.Enabled = false ;        //启用定时器，每隔一秒进行进程kill
            btn1_auto_on.Enabled = true;
            btn1_auto_off.Enabled = false ;
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)  //添加进程到关闭列表
        {
            lb_pro_tokill.Items.Add(listBox1.SelectedItem.ToString());
        }

        private void btn_pro_clear_Click(object sender, EventArgs e)
        {
            lb_pro_tokill.Items.Clear();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 about = new AboutBox1();
            about.ShowDialog();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
            //托盘处理在显示与不显示主窗体间进行切换
        {
            this.Show();

        }

        private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Hide();   
            this.Close();
        }

        private void 还原ToolStripMenuItem_Click(object sender, EventArgs e)  //托盘点击还原主窗体
        {
            this.WindowState = FormWindowState.Normal;
            this.Visible = true;
            this.Activate();
        }

        private void 主题ToolStripMenuItem_Click(object sender, EventArgs e)  //显示帮助
        {
            try
            { System.Diagnostics.Process.Start("帮助.chm"); }
            catch 
            { MessageBox.Show("帮助文件没有找到！","出错啦！",MessageBoxButtons .OK ,MessageBoxIcon.Error ); }
        }

        private void Kill_Process_FormClosing(object sender, FormClosingEventArgs e)
        {
                process_save();         
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            process_save();
            Application.Exit();
        }

        private void process_save()   //save data when form_closing
        {
            XmlDocument xmlDoc = new XmlDocument();
            //建立Xml的定义声明
            XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            xmlDoc.AppendChild(dec);
            //创建根节点
            XmlElement root = xmlDoc.CreateElement("kill_processes");
            xmlDoc.AppendChild(root);

            XmlNode processes = xmlDoc.CreateElement("processes");

            for (int i = 0; i < lb_pro_tokill.Items.Count; i++)
            {
                XmlElement pro_name = xmlDoc.CreateElement("pro_name");
                pro_name.InnerText =lb_pro_tokill .Items [i].ToString ();
                processes.AppendChild(pro_name);
            }

            XmlElement time = xmlDoc.CreateElement("time");
            time.InnerText = tb_time .Text ;

            root.AppendChild(processes);
            root.AppendChild(time);

            xmlDoc.Save(System.Environment.GetEnvironmentVariable("WINDIR") + "\\nathan_pro_kill.xml");
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                lb_pro_tokill.Items.Add(listBox1.SelectedItem.ToString());
            }
            catch { }
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            lb_pro_tokill.Items.Remove(lb_pro_tokill.SelectedItem);
        }

        private bool create_xml()
        {
            XmlDocument xmlDoc = new XmlDocument();
            //建立Xml的定义声明
            XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            xmlDoc.AppendChild(dec);
            //创建根节点
            XmlElement root = xmlDoc.CreateElement("kill_processes");
            xmlDoc.AppendChild(root);

            XmlNode processes = xmlDoc.CreateElement("processes");

                XmlElement pro_name = xmlDoc.CreateElement("pro_name");
                pro_name.InnerText = "IEXPLORE";
                processes.AppendChild(pro_name);

                XmlElement pro_info = xmlDoc.CreateElement("pro_info");
                pro_info.InnerText = "ie浏览器";
                pro_info.SetAttribute("time", "5");
                processes.AppendChild(pro_info);
     
                root.AppendChild(processes);

            XmlElement time = xmlDoc.CreateElement("time");
                time.InnerText = "1";
                root.AppendChild(time);

                xmlDoc.Save(System.Environment.GetEnvironmentVariable("WINDIR") + "\\nathan_pro_kill.xml");

            /*
             生成后的processes.xml文件内容:
             <?xml version="1.0" encoding="utf-8"?>
                <kill_processes>
                  <processes>
                    <pro_name>IEXPLORE</pro_name>
                  </processes>
                  <time>5</time>
                </kill_processes>
             */

            return true;
        }

        private void lb_pro_tokill_DoubleClick(object sender, EventArgs e)
        {
            lb_pro_tokill.Items.Remove(lb_pro_tokill.SelectedItem);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://zybsoft.com");
        }

        private void Kill_Process_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)//最小化      
            {
                this.ShowInTaskbar = false;
                this.notifyIcon1.Visible = true;
            }    
        }

        private void tb_time_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void btn_minimize_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

   }
}


//下载地址：http://94011.info/mysoft killprocess.rar Designed by Nathan Zhang QQ:5521833


